package com.khshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.pojo.Menu;
import com.khshop.service.MenuService;
import com.khshop.vo.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 菜单按钮 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-08
 */
@Api(tags = "菜单按钮 前端控制器")
@Controller
@RequestMapping("/")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("条件分页查询")
    @PostMapping("/getMenuList")
    @ResponseBody
    public AjaxResult getMenuList(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "limit",defaultValue = "15") Integer limit){
        IPage<Menu> allMenu = menuService.findAllMenu(page, limit);
        if (allMenu!=null) {
            return AjaxResult.success((int) allMenu.getTotal(), allMenu.getRecords());
        }else {
            return AjaxResult.error();
        }

    }
    @ApiOperation("增加菜单")
    @PostMapping("/addMenu")
    @ResponseBody
    public AjaxResult addMenu(@RequestBody Menu menu){
        if (menuService.addMenu(menu)){
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败，请稍后重试！");
        }
    }
    @ApiOperation("更新菜单")
    @PostMapping("/updateMenu")
    @ResponseBody
    public AjaxResult updateMenu(@RequestBody Menu menu){
        if (menuService.updateMenu(menu)){
            return  AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败，请稍后重试！");
        }
    }
    @ApiOperation("删除菜单")
    @GetMapping("/delMenu")
    @ResponseBody
    public AjaxResult delMenu(Long id){
        if (menuService.delMenu(id)){
            return AjaxResult.success("删除注册成功");
        }else {
            return AjaxResult.error("删除失败，请稍后重试！");
        }
    }


}

